﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;

namespace BizElements.Web.UI
{
    /// <summary>Form layout.</summary>
    public partial class Site : System.Web.UI.MasterPage
    {
        /// <summary>If "frameset" layout is configured then toolbar and menu cells will not be rendered.</summary>
        static readonly bool IsInFrameset = ConfigurationManager.AppSettings["WebLayout"] == "frameset";

        /// <summary>If "iframe" layout is used then menu is loaded in an iframe.</summary>
        static readonly bool IframeUsed = ConfigurationManager.AppSettings["WebLayout"] == "iframe";

        /// <summary>This member is never accessed directly. FormContext getter must always be used as it ensures that the context is instantiated/deserialized.</summary>
        WebFormContext formContext;

        /// <summary>Gets current form context. Ensures that it is initialized/deserialized. Context may be passed via session state or query string.</summary>
        private WebFormContext FormContext
        {
            get
            {
                // WebFormContext is serialized to uiWebFormContext hidden field.
                WebFormContext.EnsureFormContext(ref this.formContext, this.uiWebFormContext);
                return this.formContext;
            }
        }

        /// <summary>Hidden field where content forms store their context.</summary>
        public HiddenField WebFormContextHiddenField
        {
            get { return this.uiWebFormContext; }
        }

        /// <summary>Controls whether entire layout is rendered or only content form when in popup mode or in frames.</summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            bool isPopup = (this.FormContext.Mode == OpenFormMode.Popup);
            if (IsInFrameset || isPopup)
            {
                this.uiToolbarRow.Visible = this.uiMenuCell.Visible = false;
            }
            else
            {
                this.uiToolbarRow.Visible = this.uiMenuCell.Visible = true;
                this.uiMenu.Visible = !IframeUsed;
                this.uiMenuFrame.Visible = IframeUsed;
            }
        }
    }
}